package com.example.demo.utl;

import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Component
public  class ImageUtil {

	public  Map<String, BufferedImage> createImage() {

		String[] msg = {"1","2","3","4","5","6","7","8","9","0"};
		Random random = new Random();
		String code = "";
		for(int i = 0; i < 4; i ++) {
			int rIndex = random.nextInt(msg.length);
			code += msg[rIndex];
		}

		BufferedImage bImg = new BufferedImage(200, 40, BufferedImage.TYPE_INT_RGB);
		Graphics g = bImg.getGraphics();
		g.setColor(Color.white);
		g.fillRect(0, 0, 200, 40);

		for(int i = 0; i < code.length(); i ++) {
			g.setColor(getRandomColor());
			String s = code.charAt(i) + "";
			g.drawString(s, 40 * i + 30, 23);
		}
		
		Map<String, BufferedImage> map = new HashMap<String, BufferedImage>();
		map.put(code, bImg);
		return map;
	}

	private Color getRandomColor() {
		
		Random ran = new Random();
		Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran
				.nextInt(256));
		return color;
	}
}